﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using HHCommon;
using System.Text;

namespace HHWeb.SystemManage.DownCores
{
    public partial class List : Base
    {
        public int code = 1;
        HHBLL.DownCores B = new HHBLL.DownCores();
        HHCommon.PagePower pagepower = new PagePower();
        Help help = new Help();
        //搜索变量
        public static string SearchFileName = string.Empty;
        public static string SearchPublishTimeS = string.Empty;
        public static string SearchPublishTimeE = string.Empty;
        public static string DownCoreClassID = string.Empty;
        public string DownCoreClassNameInfo = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (help.GetHttpMethodBool())
                {
                    string message = string.Empty;
                    switch (Request.Form["Command"].ToString())
                    {
                        case "Delete":
                            message = ExeDelete();
                            break;
                        case "UpdateStatus":
                            UpdateStatus();
                            break;
                        case "Search":
                            Search();
                            break;
                        default:
                            break;
                    }
                    if (!string.IsNullOrEmpty(message))
                    {
                        if (message.EndsWith("成功！"))
                        {
                            MessageBox.ResponseScript(this.Page, "alert('" + message + "')");
                            Bind(Convert.ToInt32(ViewState["NowPage"]), 15);
                        }
                        else
                            MessageBox.ShowAndRedirect(this.Page, message, "List.aspx");
                    }
                }
                else
                {
                    if (!IsPostBack)
                    {
                        ViewState["NowPage"] = 1;
                        ViewState["Where"] = "";
                        Bind(Convert.ToInt32(ViewState["NowPage"]), 15);
                    }
                }
            }
            catch
            {
                ViewState["NowPage"] = 1;
                ViewState["Where"] = "";
                Bind(Convert.ToInt32(ViewState["NowPage"]), 15);
            }
            finally
            {
                DownCoreClassNameInfo= pagepower.GetPagePowerInfo("Select [ID] as ID, [Title] as Name, [ParentID] as ParentID from DownCoreClass with(nolock) where Status=1 order by sort desc");
            }
        }
        public void Bind(int NowPage, int PageSize)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@tbname", SqlDbType.NVarChar,50),
					new SqlParameter("@FieldKey", SqlDbType.NVarChar,1000),
					new SqlParameter("@NowPage", SqlDbType.Int,4),
					new SqlParameter("@PageSize", SqlDbType.Int,4),
					new SqlParameter("@FieldShow", SqlDbType.NVarChar,1000),
					new SqlParameter("@FieldOrder", SqlDbType.NVarChar,1000),
					new SqlParameter("@Where", SqlDbType.NVarChar,1000),
                    new SqlParameter("@Totalcount", SqlDbType.Int,4)};
            parameters[0].Value = "DownCores";
            parameters[1].Value = "ID";
            parameters[2].Value = NowPage;
            parameters[3].Value = PageSize;
            parameters[4].Value = "";
            parameters[5].Value = "Sort desc,PublishTime desc";
            parameters[6].Value = ViewState["Where"].ToString();
            parameters[7].Direction = ParameterDirection.Output;
            Repeater1.DataSource = HHDBUtility.DbHelperSQL.RunProcedure("P_Pageing", parameters);
            Repeater1.DataBind();
            AspNetPager1.AlwaysShow = true;
            AspNetPager1.PageSize = PageSize;
            AspNetPager1.RecordCount = Convert.ToInt32(parameters[7].Value);
        }
        //AspNetPager分页事件
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            code = 1;
            ViewState["NowPage"] = e.NewPageIndex;
            Bind(Convert.ToInt32(ViewState["NowPage"]), 15);
        }

        #region //删除数据
        public string ExeDelete()
        {
            if (B.DeleteList(Request.Form["hdDelete"].ToString()))
                return "删除成功！";
            else
                return "删除失败！";
        }
        #endregion

        #region //修改状态
        public void UpdateStatus()
        {
            int ID = Convert.ToInt32(Request.Form["hdDelete"]);
            int Status = Convert.ToInt32(Request.Form["HDStatus"]);
            if (Status == 1)
                Status = 0;
            else
                Status = 1;
            B.UpdateStatusByID(ID, Status);
            Bind(Convert.ToInt32(ViewState["NowPage"]),15);
        }
        #endregion

        #region //搜索查询
        public void Search()
        {
            StringBuilder Wherestr=new StringBuilder();
            SearchFileName = Request.Form["SearchFileName"].ToString();
            SearchPublishTimeS = Request.Form["SearchPublishTimeS"].ToString();
            SearchPublishTimeE = Request.Form["SearchPublishTimeE"].ToString();
            DownCoreClassID = Request.Form["DownCoreClassID"].ToString();
            Wherestr.Append(" 1=1");
            if (!string.IsNullOrEmpty(SearchFileName))
                Wherestr.Append(" and FileName like '%" + SearchFileName + "%'");
            if (!string.IsNullOrEmpty(SearchPublishTimeS))
                Wherestr.Append(" and PublishTime >='" + SearchPublishTimeS + "'");
            if (!string.IsNullOrEmpty(SearchPublishTimeE))
                Wherestr.Append(" and PublishTime <='" + SearchPublishTimeE + "'");
            if (DownCoreClassID!="0")
                Wherestr.Append(" and DownCoreClassID =" + DownCoreClassID + "");
            ViewState["Where"] = Wherestr.ToString();
            ViewState["NowPage"] =1;
            Bind(Convert.ToInt32(ViewState["NowPage"]), 15);
        }
        #endregion
    }
}